﻿#include "Widget.h"
#include <QDebug>
#include <QLabel>
#include <QGridLayout>
#include <QVBoxLayout>
#include <QApplication>
#include <QFontDatabase>
#include <QScrollArea>

#define EACH_ROW_ICON_COUNT    10

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    this->resize(700, 600);

    if (!_fontIcon.importFontIcon(":/images/fontawesome-webfont.ttf"))
        qDebug() << QString::fromLocal8Bit("加载图标字体库失败！");
    else
        init();
}

Widget::~Widget()
{
}

void Widget::init()
{
    QScrollArea *pArea = new QScrollArea(this);
    QWidget *pContentWidget = new QWidget;

    QGridLayout *pGLayout = new QGridLayout(pContentWidget);

    int count = 700;
    int row = 0;
    int column = 0;
    for (int i = 0; i < count; i++)
    {
        QLabel *pLabel = new QLabel(this);
        pLabel->setFixedSize(64, 64);
        pLabel->setAlignment(Qt::AlignCenter);

        QFont font = _fontIcon.getFont(64);
        pLabel->setFont(font);
        pLabel->setText(_fontIcon.getIconText(i));
        pLabel->setToolTip(_fontIcon.getIconTip(i));

        row = i / (EACH_ROW_ICON_COUNT - 1);
        column = i % (EACH_ROW_ICON_COUNT - 1);

        QPalette palette;
        if (row % 3 == 0)
            palette.setColor(QPalette::WindowText, Qt::red);
        else if (row % 3 == 1)
            palette.setColor(QPalette::WindowText, Qt::green);
        else
            palette.setColor(QPalette::WindowText, Qt::blue);
        pLabel->setPalette(palette);

        pGLayout->addWidget(pLabel, row, column, 1, 1);
    }
    pArea->setWidget(pContentWidget);

    QVBoxLayout *pVLayout = new QVBoxLayout(this);
    pVLayout->addWidget(pArea);
}

